package com.lipeng.service.impl;

import com.lipeng.bean.User;
import com.lipeng.mapper.UserMapper;
import com.lipeng.service.UserService;
import com.lipeng.utils.AjaxResult;
import com.lipeng.utils.IPUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by dllo on 18/3/3.
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public List<User> selectAllUser() {
        return userMapper.selectAllUser();
    }

    @Override
    public List<User> selectUserByName(String name) {
        return userMapper.selectUserByName(name);
    }

    @Override
    public int addUser(User user) {
        return userMapper.insertSelective(user);
    }

    @Override
    public int delUser(Integer uid) {
        return userMapper.deleteByPrimaryKey(uid);
    }

    @Override
    public int changeState(Integer ustate,Integer uid) {
        return userMapper.changeState(ustate,uid);
    }

    @Override
    public User findUserByName(String name) {
        return userMapper.findUserByName(name);
    }

    @Override
    public AjaxResult doLogin(String uname, String upsw) {
        AjaxResult result = new AjaxResult();
        User user = userMapper.doLogin(uname,upsw);
        if (user == null){
            result.setCode("0");
            result.setMessage("用户名或密码错误");
            return result;
        }
        result.setCode("1");
        result.setMessage("登录成功");
        result.setData(user);
        return result;
    }

    @Override
    public AjaxResult updateUser(User user) {
        AjaxResult result = new AjaxResult();
        int num = userMapper.updateByPrimaryKeySelective(user);
        if (num<1){
            result.setCode("0");
            result.setMessage("修改失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("修改成功");
        return result;
    }

    @Override
    public AjaxResult selectUserById(Integer uid) {
        AjaxResult result = new AjaxResult();
        User user = userMapper.selectByPrimaryKey(uid);
        if (user == null){
            result.setCode("0");
            result.setMessage("查询失败");
            return result;
        }

        user.setUpic(IPUtils.getIP()+":8080"+user.getUpic());

        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(user);
        return result;
    }
}
